package at.main.util;

import java.util.Vector;

import at.main.android.Graphics;
import at.main.android.Image;

public class ManagerFont 
{
	 	public static final int ALIGN_LEFT = 0;
	    
		public static final int ALIGN_RIGHT = 1;
		
		public static final int ALIGN_CENTER = 2;
		
	    public static final int COLOR_RED = 1;
	    
		public static final int COLOR_BLUE = 2;
		
	    public static Image imgFontRed,imgFontBlue;
	    
	    public static final String CHAR_MAP = " 0123456789.,:!?()+-*/#$%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZÁÀẢÃẶĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ\\"+'"'+"@<=>;&'";
	    //   4,4,9,6,6,6,2,7,1
	    public static final int[] CHAR_WIDTH = { 2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 2, 2, 2, 5, 6, 6, 7, 6, 6, 5, 8, 5, 9, 6, 6, 5, 6, 6, 4, 6, 6, 2, 3, 6, 2, 10, 6, 6, 6, 6, 4, 5, 4, 6, 6, 8, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 7, 6, 6, 3, 2, 2, 4, 2, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6, 6, 7, 7, 6, 6, 7, 5, 5, 7, 7, 4, 5, 6, 5, 9, 6, 7, 6, 7, 7, 6, 6, 7, 6, 10, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 6, 5, 5, 5, 5, 4, 4, 4, 5, 4, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 6, 6, 6, 6, 6, 8,4,4,9,6,6,6,2,7,1};

	    public static int CHAR_HEIGHT = 13;
	    
	    static {
			try {
				imgFontRed = Image.createImage("/redfont.png");
				imgFontBlue = Image.createImage("/bluefont.png");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	    
	    public static void drawString(Graphics g, String st, int x, int y, int align, int color) {
			int pos;
			int x1 = 0;
			int len = st.length();
			switch (align) {
			case ALIGN_RIGHT:
				x1 = x - stringWidth(st);
				break;
			case ALIGN_CENTER:
				x1 = x - (stringWidth(st) >> 1);
				break;
			default: // case LEFT
				x1 = x;
				break;
			}
			switch (color) {
			case COLOR_RED:
				for (int i = 0; i < len; i++) {
					pos = CHAR_MAP.indexOf(st.charAt(i));
					if (pos == -1)
						pos = 0;
					else
						g.drawRegion(imgFontRed, 0, pos * CHAR_HEIGHT, CHAR_WIDTH[pos], CHAR_HEIGHT, 0, x1, y, 20);
					x1 += CHAR_WIDTH[pos] + 1;
				}
				break;
			case COLOR_BLUE:
				for (int i = 0; i < len; i++) {
					pos = CHAR_MAP.indexOf(st.charAt(i));
					if (pos == -1)
						pos = 0;
					else
						g.drawRegion(imgFontBlue, 0, pos * CHAR_HEIGHT, CHAR_WIDTH[pos], CHAR_HEIGHT, 0, x1, y, 20);
					x1 += CHAR_WIDTH[pos] + 1;
				}
				break;
			}
		}
	    
	    public static int stringWidth(String st)
	    {
	    	int pos;
	        int len = 0;
	        for (int i = 0; i < st.length(); i++)
	        {
	            pos = CHAR_MAP.indexOf(st.charAt(i));
	            pos = pos == -1 ? 0 : pos;
	            len += (CHAR_WIDTH[pos] + 1);
	        }
	        return len;
	    }
	    public static String[] splitStrInLine(String src, int lineWidth)
	    {
	        Vector list = new Vector();
	        int srclen = src.length();
	        String tem = "";
	        int start = 0, end = 0;
	        while (true)
	        {
	            while (stringWidth(tem) < lineWidth)
	            {
	                tem += src.charAt(end);
	                end++;
	                if (end == srclen - 1)
	                    break;
	            }
	            if (end != srclen - 1 && (src.charAt(end + 1) != ' '))
	            {
	                int endAnyway = end;
	                while (true)
	                {
	                    if (src.charAt(end + 1) == ' ' && src.charAt(end) != ' ')
	                        break;
	                    if (end == start)
	                        break;
	                    end--;
	                }
	                if (end == start)
	                    end = endAnyway;
	            }
	            list.addElement(src.substring(start, end + 1));
	            if (end == srclen - 1)
	                break;
	            start = end + 1;
	            while (start != srclen - 1 && src.charAt(start) == ' ')
	                start++;
	            if (start == srclen - 1)
	                break;
	            end = start;
	            tem = "";
	        }
	        String[] strs = new String[list.size()];
	        for (int i = 0; i < list.size(); i++)
	        {
	            strs[i] = (String) list.elementAt(i);
	        }
	        return strs;
	    }
	    
	}
